package com.lyw.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyw.common.valid.groups.AddGroup;
import com.lyw.common.valid.groups.UpdateGroup;
import com.lyw.common.valid.groups.UpdateShowStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lyw.gulimall.product.entity.BrandEntity;
import com.lyw.gulimall.product.service.BrandService;
import com.lyw.common.utils.R;


/**
 * 品牌
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2022-05-31 11:35:50
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                  @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                  @RequestParam(value = "key", required = false) String name){
        IPage<Map<String, Object>> list = brandService.queryPage(page, limit, name);
        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * @Valid: 校验实体类
     * 在对应的控制器方法的参数上给要校验的实体类添加`@Valid`注解,`BindingResult`对象可以获取的校验信息
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:brand:save")
    // @Validated(value = AddGroup.class) spring 家提供的校验注解，根据 value 的值分组, 默认不校验没添加分组的属性
    public R save(@Validated({ AddGroup.class }) @RequestBody BrandEntity brand/*, BindingResult result*/){
        /*if (result.hasErrors()) {
            Map<String, String> map = new HashMap<>()
            result.getFieldErrors().forEach(item -> {
                // 获取错误属性
                String field = item.getField()
                // 获取错误消息
                String message = item.getDefaultMessage()
                map.put(field, message)
            })
            return R.error(400, "提交的数据不合法").put("data", map)
        }*/
        brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@Validated({ UpdateGroup.class }) @RequestBody BrandEntity brand){
        brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/showStatus")
    // @RequiresPermissions("product:brand:update")
    public R showStatus(@Validated({UpdateShowStatusGroup.class})@RequestBody BrandEntity brand){
        brandService.updateShowStatusById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
